//
//  BeePhotoImageView.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/1.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "BeePhotoImageView.h"
#import "UIImageView+WebCache.h"

@implementation BeePhotoImageView

-(instancetype)initWithFrame:(CGRect)frame photoStyle:(PhotoStyle)style
{
    self = [super initWithFrame:frame];
    
    if (self) {

        if (style == BeePhotoCellStyle) {
            //默认设为圆角 如果卡可用img遮挡代替
            UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            bg.image = [UIImage imageNamed:@"photo_bg"];
            [self addSubview:bg];
        }
        else
        {
            self.layer.cornerRadius = frame.size.width/2;
            self.clipsToBounds = YES;

        }
        
        
        
        self.backgroundColor = FBEE_COLOR_PLACEHOLDER;
        
    }
    
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame imgUrl:(NSString *)url photoStyle:(PhotoStyle)style
{
    self = [self initWithFrame:frame photoStyle:style];
    
    if (self) {
        
        [self sd_setImageWithURL:[NSURL URLWithString:url]];
        
    }
    
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame imgByTitle:(NSString *)title photoStyle:(PhotoStyle)style
{
    self = [self initWithFrame:frame photoStyle:style];
    
    if (self) {
        
        cl = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.addFont([UIFont boldSystemFontOfSize:(frame.size.height -4)/2]);
            make.addTextColor(FBEE_COLOR_TEXT);
            make.addText([title firstCharactor]).addTextAlignment(NSTextAlignmentCenter);
            make.setFrame(CGRectMake(0, 0, frame.size.width, frame.size.height));
        }];
        [self addSubview:cl];
        [self setBgColor];
        
    }

    return self;
}

-(void)setTitle:(NSString *)title
{
    self.image = nil;
    
    if (!cl) {
        cl = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.addFont([UIFont boldSystemFontOfSize:(self.height -4)/2]);
            make.addTextColor(FBEE_COLOR_TEXT);
            make.addText([title firstCharactor]).addTextAlignment(NSTextAlignmentCenter);
            make.setFrame(CGRectMake(0, 0, self.width, self.height));
        }];
    }
    [self addSubview:cl];
    cl.text = [title firstCharactor];
    [self setBgColor];
}

-(void)setUrl:(NSString *)url
{
    [self sd_setImageWithURL:[NSURL URLWithString:url]];
}


-(void)setBgColor
{
    if (![NSString isEmptyString:cl.text]) {
      
        
        cl.textColor = UIColorFromRGB(0xBDBBC7);
        self.backgroundColor = UIColorFromRGB(0xE2E1E9);
        return;
//        /*
//            A - Z
//            65 - 90
//         */
//        int asciiCode = [cl.text characterAtIndex:0];
//
//        //A-D
//        if (asciiCode >= 65 && asciiCode <69) {
//            cl.textColor = UIColorFromRGB(0xEF8577);
//            self.backgroundColor = UIColorFromRGBA(0xFF5842,0.5);
//            return;
//        }
//
//        //E-H
//        if (asciiCode >= 69 && asciiCode <73) {
//            cl.textColor = UIColorFromRGB(0xE2A263);
//            self.backgroundColor = UIColorFromRGBA(0xF89128,0.5);
//            return;
//        }
//
//        //I-L
//        if (asciiCode >= 73 && asciiCode <77) {
//            cl.textColor = UIColorFromRGB(0xEDD559);
//            self.backgroundColor = UIColorFromRGBA(0xFFE342,0.5);
//            return;
//        }
//
//        //M-P
//        if (asciiCode >= 77 && asciiCode <81) {
//            cl.textColor = UIColorFromRGB(0xB7E972);
//            self.backgroundColor = UIColorFromRGBA(0xB0FF42,0.5);
//            return;
//        }
//
//        //Q-T
//        if (asciiCode >= 81 && asciiCode <85) {
//            cl.textColor = UIColorFromRGB(0x81DBC3);
//            self.backgroundColor = UIColorFromRGBA(0x42FFCE,0.5);
//            return;
//        }
//
//        //U-X
//        if (asciiCode >= 85 && asciiCode <=89) {
//            cl.textColor = UIColorFromRGB(0x7E8ADD);
//            self.backgroundColor = UIColorFromRGBA(0x4259FF,0.5);
//            return;
//        }
//
//        //Y-Z
//        cl.textColor = UIColorFromRGB(0xCE87E3);
//        self.backgroundColor = UIColorFromRGBA(0xD442FF,0.5);

        
    }
    
}

@end
